package com.sjb.sixteen_journey_backend.controller;


import com.sjb.sixteen_journey_backend.common.ImagesProperties;
import com.sjb.sixteen_journey_backend.common.R;
import com.sjb.sixteen_journey_backend.entity.User;
import com.sjb.sixteen_journey_backend.entity.dto.LoginDto;
import com.sjb.sixteen_journey_backend.entity.dto.UpdatePwdDto;
import com.sjb.sixteen_journey_backend.service.IUserService;
import com.sjb.sixteen_journey_backend.utils.AliOssUtil;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.util.UUID;

/**
 * 用户表
 *
 * @author jcz
 * @since 2024-06-24
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService IUserService;

    @Autowired
    private ImagesProperties imagesProperties;

    /**
     * 判断用户名是否被占用
     * @param username
     * @return
     */
    @GetMapping("/isExistUserName")
    public R isExistUserName(@RequestParam String username) {
        return IUserService.isExistUserName(username);
    }

    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @PostMapping("/register")
    public R register(@Validated(User.Register.class) @RequestBody User user) {
        return IUserService.register(user);
    }

    /**
     * 登录
     *
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
    public R<User> login(@Validated @RequestBody LoginDto loginDto) throws Exception {
        return IUserService.login(loginDto);
    }

    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    @GetMapping("/getUserById/{id}")
    public R<User> getUserById(@PathVariable("id") Integer id) {
        return R.success(IUserService.getUserById(id),"查询成功");
    }




//    @PostMapping("/upload")
//    public R<String> upload(@RequestParam MultipartFile file) throws Exception {
//        try {
////            把文件的内容存储到本地磁盘上  file.getOriginalFilename文件的原始名字
//            String originalFilename = file.getOriginalFilename();
////            保证文件的名字是唯一的,从而防止文件覆盖
//            String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
////            存储在本地
//            String realPath = System.getProperty("user.dir") +imagesProperties.getRootPath()+imagesProperties.getAvatar();
////            String realPath = System.getProperty("user.dir") + "\\src\\main\\resources\\static\\images\\avatar\\";
//            file.transferTo(new File(realPath + filename));
//            return R.success(imagesProperties.getVisitImagePath()+imagesProperties.getAvatar() + filename, "url访问成功");
////            存储在阿里云云服务器上 oss
////            String url = AliOssUtil.uploadFile(filename,file.getInputStream());
////            return R.success(url,"图片上传成功");
//        } catch (Exception e) {
//            return R.error("图片上传失败！");
//        }
//    }

    /**
     * 修改密码
     *
     * @param updatePwdDto
     * @return
     */
    @PatchMapping("/updatePwd")
    public R updatePwd(@RequestBody UpdatePwdDto updatePwdDto) {
        return IUserService.updatePwd(updatePwdDto);
    }

    /**
     * 注销用户
     *
     * @return 记得在前端删除token
     */
    @DeleteMapping("/deleteUser")
    public R deleteUser() {
        return IUserService.deleteUser();
    }

    /**
     * 修改用户头像
     *
     * @return 先从上传头像接口中获取到头像url, 再使用前端接口修改头像
     */
    @PostMapping("/updateUserAvatar")
    public R<User> updateUserAvatar(@RequestParam MultipartFile avatar) {
        return IUserService.updateUserAvatar(avatar);
    }

    /**
     * 修改用户名称
     *
     * @return
     */
    @PatchMapping("/updateUserUsername")
    public R<User> updateUserUsername(@RequestParam String username) {
        return IUserService.updateUserUsername(username);
    }

    /**
     * 修改用户性别
     *
     * @return
     */
    @PatchMapping("/updateUserGender")
    public R<User> updateUserGender(@RequestParam Integer gender) {
        return IUserService.updateUserGender(gender);
    }

    /**
     * 修改用户住址
     *
     * @return
     */
    @PatchMapping("/updateUserAddress")
    public R<User> updateUserAddress(@RequestParam String Address) {
        return IUserService.updateUserAddress(Address);
    }

    /**
     * 修改用户签名
     *
     * @return
     */
    @PatchMapping("/updateUserSignature")
    public R<User> updateUserSignature(@RequestParam String signature) {
        return IUserService.updateUserSignature(signature);
    }

    /**
     * 根据用户名或者手机号查询用户
     *
     * @param data
     * @return
     */
    @GetMapping("/getUserByUserNameOrPhone/{data}")
    public R<User> getUserByUserNameOrPhone(@PathVariable("data") String data) {
        return IUserService.getUserByUserNameOrPhone(data);
    }


}
